
/************头文件***************/
#include "ui_styles.h"
#include <QApplication>

#include <QFile> //文件加载风格可以用到

#ifdef Q_OS_WIN
#include <dwmapi.h>
#include <windows.h>
#endif

/************宏定义***************/
#ifdef Q_OS_WIN
#ifndef DWMWA_USE_IMMERSIVE_DARK_MODE
#define DWMWA_USE_IMMERSIVE_DARK_MODE 20
#endif
#endif


/************全局变量*************/
bool isDarkMode = false; // 记录当前模式
QString lightStyleSheet = R"()";
QString darkStyleSheet = R"()";
/************静态变量*************/


/**************函数**************/


#ifdef Q_OS_WIN
void setDarkMode(QWidget* window, bool enable) {

    setDarkStyle(enable);
    HWND hwnd = reinterpret_cast<HWND>(window->winId());
    BOOL dark = enable;
    DwmSetWindowAttribute(
        hwnd,
        DWMWA_USE_IMMERSIVE_DARK_MODE,
        &dark,
        sizeof(dark)
        );
}
#else
void setDarkMode(QWidget* window, bool enable)
{
    setDarkStyle(enable);
}
#endif

void setDarkStyle(bool enable) {
    if(enable)
    {
        qApp->setStyleSheet(darkStyleSheet); // 全局应用 Light Mode
    }
    else
    {
        qApp->setStyleSheet(lightStyleSheet); // 全局应用 Light Mode

    }
}


QString loadStyleSheet(const QString &filePath)
{
    QFile styleFile(QCoreApplication::applicationDirPath() +"/"+filePath);
    if(!styleFile.open(QFile::ReadOnly))
    {
        qWarning() << "Could not open style file:" << filePath;
        return QString();
    }

    QTextStream styleStream(&styleFile);
    QString styleSheet = styleStream.readAll();
    styleFile.close();
    return styleSheet;
}

uint8_t set_light_dark_StyleSheet(const QString &lightfilePath,const QString &darkfilePath)
{
    uint8_t res = 0;
    QFile styleFile(QCoreApplication::applicationDirPath() + "/" + lightfilePath);
    if(styleFile.open(QFile::ReadOnly))
    {
        QTextStream styleStream(&styleFile);
        lightStyleSheet = styleStream.readAll();
        res = 1<<1;
    }
    styleFile.close();


    QFile styleFile1(QCoreApplication::applicationDirPath() + "/" + darkfilePath);
    if(styleFile1.open(QFile::ReadOnly))
    {
        QTextStream styleStream(&styleFile1);
        darkStyleSheet = styleStream.readAll();
        res += 1<<2;
    }
    styleFile1.close();

    return res;
}


//设置透明度
void enableAcrylicEffect(QWidget* widget,qreal level)
{
    // 方法1：设置窗口整体透明度（0~1.0）
    if (widget) {  // 安全检查
        widget->setWindowOpacity(level);
    }
}



